package org.wotou.authority.web.controller.wtuser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wotou.authority.domain.wtuser.wtuserDO;
import org.wotou.authority.dto.ResultDTO;
import org.wotou.authority.dto.UserCookieDTO;
import org.wotou.authority.service.wtuser.WtuserService;
import org.wotou.authority.utils.Constant;
import org.wotou.authority.utils.EhcacheUtil;
import org.wotou.authority.utils.IpUtil;
import org.wotou.authority.utils.JwtUtil;
import org.wotou.authority.web.controller.BaseController;
import org.wotou.authority.web.enums.LoginStateEnum;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户controller
 *
 * @author zhangQ
 * @date 2017/12/18
 */
@RestController
@RequestMapping("/wtuser")
public class WtuserController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(WtuserController.class);

    @Autowired
    private WtuserService wtuserService;

    @RequestMapping(value = "/checkLogin")
    public ResultDTO checkLogin(@RequestBody wtuserDO wtuser) {
        ResultDTO resultDTO = ResultDTO.getResultSuccess(null);
        List<wtuserDO> wtuserList = wtuserService.queryByCondition(wtuser);
        if (wtuserList.size() <= 0) {
            resultDTO.setServiceCode(LoginStateEnum.USEISNO.getCode());
            resultDTO.setMessage(LoginStateEnum.USEISNO.getMsg());
            return resultDTO;
        }
        List<wtuserDO> wtuserListFilter = wtuserList.stream().filter((p) -> (wtuser.getPassword().equals(p.getPassword()))).collect(Collectors.toList());
        if (wtuserListFilter.size() <= 0) {
            resultDTO.setServiceCode(LoginStateEnum.PWDWRONG.getCode());
            resultDTO.setMessage(LoginStateEnum.PWDWRONG.getMsg());
            return resultDTO;
        }
        // 获得实体
        wtuserDO resWtuser = wtuserListFilter.get(0);
        if (!Constant.STATE_ENABLE.equals(resWtuser.getState())) {
            resultDTO.setServiceCode(LoginStateEnum.ISSTATENULL.getCode());
            resultDTO.setMessage(LoginStateEnum.ISSTATENULL.getMsg());
        }
        // 放入缓存
/*        wtuserDO cacheWtuser = (wtuserDO) EhcacheUtil.get("wtuser", "get" + resWtuser.getId());
        if (null == cacheWtuser) {

        }*/
        // 加入缓存，建立JWT信息
        EhcacheUtil.put("wtuser", "get" + resWtuser.getId(), resWtuser);
        UserCookieDTO userCookieDTO = new UserCookieDTO(resWtuser.getId(), new Date(), IpUtil.getIp2(this.request));
        this.response.setHeader(Constant.ACCESS_TOKEN, JwtUtil.sign(userCookieDTO, (new Date()).getTime()));
        this.response.setHeader("SEE_KK", "123456");
        return resultDTO;
    }

    @RequestMapping(value = "/addUser")
    public void addUser() {
        wtuserDO wtuser = new wtuserDO();
        wtuser.setUserName("0000");
        wtuser.setPassword("0000");
        wtuserService.add(wtuser);
    }
}
